<?php

namespace addons\venue\controller;

use app\admin\model\venue\lesson\UserLesson;
use app\common\controller\Api;
use app\admin\model\venue\lesson\Lesson as L;
use app\admin\model\venue\order\Order;
use think\Request;

class Lesson extends Api
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['list','detail'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    protected $config = "";

    public function __construct(Request $request = null)
    {
        $this->config = get_addon_config("venue");
        parent::__construct($request);
    }

    /*
     * 课程列表
     */
    public function list(){
        $lon = $this->request->header("lon")??'113.663221';
        $lat = $this->request->header("lat")??'34.7568711';
        $type_id        = $this->request->post('type_id');      //类型id
        $venue_id       = $this->request->post('venue_id');     //场馆id
        $order          = $this->request->post('order')??"asc"; //价格
        $page           = $this->request->post('page')??1;      //页码
        $limit          = $this->request->post('limit')??10;     //每页显示条数
        $where = [];
        if ($type_id){
            $where['type_id'] = $type_id;
        }
        if ($venue_id){
            $where['venue_id'] = $venue_id;
        }

        $data = L::where('lesson.state',"=","1")
            ->with(['venue'])
            ->where($where)
            ->field("venue.name as vname,venue.address as vaddress")
            ->paginate(["page"=>$page,"list_rows"=>$limit]);
        foreach ($data as $row) {
            $row->image = cdnurl($row->image,true);
            $row->getRelation('venue')->visible(['name']);
            $row->details_json = json_decode($row->details_json,true);
            $price = array_column( $row->details_json , 'price' );
            $row->min_price        = round(min($price), 2);
            $row->max_price        = round(max($price),2);
            $lon = intval($lon);
            $lat = intval($lat);
            $row->venue->lon = intval($row->venue->lon);
            $row->venue->lat = intval($row->venue->lat);
            $row->distance = venue_get_distance([$lon,$lat],[$row->venue->lon,$row->venue->lat],false,0);


            $row->hidden(["venue","type_id","venue_id","content_image","name","tel","weekdata","description","address","details_json","remark","createtime","updatetime","deletetime","weigh","state"]);
        }
        $data = $data->toArray();
        if ($order == "asc"){
            array_multisort ( array_column( $data['data'] , 'min_price' ),SORT_ASC,$data['data']);
        }else{
            array_multisort ( array_column( $data['data'] , 'max_price' ),SORT_DESC,$data['data']);
        }

        $result = array("total" => $data['total'], "rows" => $data['data']);
        $this->success('返回成功',$result);
    }

    /*
     * 课程详情
     */
    public function detail(){
        $id   = $this->request->post('id');
        if (!$id){
            $this->error("缺少课程id");
        }
        $data = L::where('lesson.state',"=","1")
            ->with(['venue'])
            ->where('lesson.id',$id)
            ->field("venue.name as vname")
            ->find();

        $data->getRelation('venue')->visible(['name']);
        $data->hidden(['createtime',"updatetime","deletetime","weigh","state","venue","type_id","venue_id"]);
        $data->details_json = json_decode($data->details_json,true);
        $detailjson = $data->details_json;
        foreach ($detailjson as &$v){
            $v['image'] = cdnurl($v['image'],true);
        }
        $data->details_json = $detailjson;
        $price = array_column( $data->details_json , 'price' );
        $data->min_price        = round(min($price), 2);
        $data->max_price        = round(max($price), 2);
        $data->lesson_type  = implode(',',array_keys($data->details_json));
        $data->enrollment   = Order::where(["goods_id"=>$id,"order_type"=>3])->where("status","not in",1)->group("user_id")->count();
        $data->content_image = cdnurl($data->content_image,true);
        $weekdata = ['1'=>"周一","2"=>"周二","3"=>"周三","4"=>"周四","5"=>"周五","6"=>"周六","7"=>"周日"];
        $w = [];
        $w1 = explode(",",$data->weekdata);
        foreach ($w1 as $vv){
            $w[] = $weekdata[$vv];
        }
        $data->weekdata = implode(",",$w);
        $this->success('返回成功',$data);
    }

    /*
     * 我的课程列表
     */
    public function myList(){
        $userId = $this->auth->id;
        $data = UserLesson::with(["lesson"])
            ->where("user_id",$userId)
            ->field("lesson.id,lesson.title,lesson.image,lesson.tags,lesson.workrange,age")
            ->select();
        foreach ($data as $row) {
            $row->hidden(['lesson',"createtime","lesson_id","user_id"]);
        }
        $this->success('返回成功',$data);
    }




}
